package com.tian.enums;

import java.time.LocalDate;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-08-19 22:38
 * {@code @version:} 1.0
 */
public enum ChargeCouponConditionExpireUnitEnum {
    DAY(1),
    MONTH(2),
    YEAR(3);

    private final Integer code;

    ChargeCouponConditionExpireUnitEnum(Integer code) {
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public static ChargeCouponConditionExpireUnitEnum getByCode(Integer code) {
        for (ChargeCouponConditionExpireUnitEnum value : values()) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        return null;
    }

    public static LocalDate calculateExpireDate(LocalDate startDate, int expireUnit, int expire) {
        ChargeCouponConditionExpireUnitEnum conditionExpireUnit = getByCode(expireUnit);
        if (conditionExpireUnit == null) {
            return null;
        }
        switch (conditionExpireUnit) {
            case DAY:
                return startDate.plusDays(expire);
            case MONTH:
                return startDate.plusMonths(expire);
            case YEAR:
                return startDate.plusYears(expire);
            default:
                return null;
        }
    }
}
